<%@ page import="org.biojava.dasobert.dasregistry.*" %>
<jsp:useBean id="register" class="dasregistry.registryBean" scope="session"/>
<%@ page import="java.util.List,java.util.ArrayList,java.util.Iterator"%>
<%@ page import="org.biojava.services.das.registry.UserManager" %>
<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.dao.ProjectBean"%>
<%@ include file="sangerheader.jsp"%>
<jsp:useBean id="coord" class="dasregistry.coordBean" scope="page"/>
<% 
  
	String action = request.getParameter("cmd");
	if ( action == null ) 
		action = "claim";
	
	%>
	
<% 

   String title="claim DAS source";

   if ( action.equals("share") )
	   title="share ownership";
   
	String header = title;

%>



	<% 
	String[] dsIDS    = request.getParameterValues("DS_ID");
	
	if ( (dsIDS == null) || (dsIDS.length) == 0 ) {
		response.sendRedirect("listServices.jsp");	
	}
	
	List dasSources = new ArrayList();
	if (dsIDS != null){
		for ( int i=0;i< dsIDS.length;i++){
			String dsID = dsIDS[i];
			DasSource ds = register.getDasSource(dsID);
			dasSources.add(ds);
			
		}
	}
	
	
	
	
	BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
	UserManager manager=(UserManager)ctx.getBean("userManager");
	
%>


<div id="main">
<%  if ( action.equals("share") ) {%>
	<h2>Share ownership of DAS source</h2>
<%} else {%>
	<h2>Claim ownership of DAS source</h2>
<%} %>
<form id="dassourceaction" name="dassourceaction" action="DasSourceAction" method="POST" >

<%   if ( action.equals("share") ) { %>
	<input type="hidden" name="action" value="shareAction"/>	
<% } else { %>
	<input type="hidden" name="action" value="claimAction"/>	
<% } %>	

<%@ include file="sangertablestart.jsp" %>

<tr id="row0">
<td>ID</td>
<td>nickname</td>
<td>URL</td>
<td>Project</td>
<%   if ( action.equals("share") ) { 
%>
	<td>s</td>
<% } else { %>
	<td>enter passkey</td>
<% } %>	
<td>&nbsp;</td>
</tr>

<%
	Iterator iter = dasSources.iterator();
	int count=-1;
	while (iter.hasNext()){
		count++;
		DasSource ds = (DasSource) iter.next();
		%>
<input type="hidden" name="DS_ID" value="<%=ds.getId()%>"/>		
<% if (count % 2 != 0) {
%>
<tr id="row1" valign="top">
	<%
	} else {
	%>

<tr id="row2" valign="top">
<%} %>

<td><a href="showdetails.jsp?auto_id=<%=ds.getId() %>"><%=ds.getId() %></a></td>
<td><a href="showdetails.jsp?auto_id=<%=ds.getId() %>"><%=ds.getNickname() %></a></td>
<td><%=ds.getUrl() %></td>
<td><%
	ProjectBean tp = coord.getProjectBeanForURL(ds.getUrl());
	if (tp != null) {
		%><img src="<%= tp.getIconURL()%>"/><%
	}
%></td>

<td>
<%   if ( action.equals("claim") ) { %>
	<input type="password" name="passkey" size=10/>
<%} %>

</td>

<td>
<%   if ( action.equals("claim") ) { %>
<a href="reminder.jsp?url=<%=ds.getUrl() %>">forgot passkey</a>
<%} %>
</td>
</tr>
		<%
}
%>
<%@ include file="sangertableend.jsp" %>

<h3>Choose user</h3>
<% if (action.equals("share")) {
%>
<p>
<select name="user_id">
	<%
	
	RegistryUser[] users = manager.getAllUsers();
	
	for (int u=0; u < users.length ; u++){
		RegistryUser tmp = users[u];
		
		%>
		<option value="<%=tmp.getUserID() %>"><%=tmp.getEmail() %></option>
		<% 
	}
	
	
	%>
</select>
<%} %>

</p>


<%   if ( action.equals("claim") ) {%>
	<input type="submit" value="claim ownership"/>
<% } else {%>
	<input type="submit" value="share ownership"/>
<% } %>
</form>
<p>

</p>
</div>

<%@ include file="sangerfooter.jsp" %>


